//
//  USMomentCommentCell.m
//  us
//
//  Created by karry on 2025/6/7.
//

#import "USMomentCommentCell.h"

@interface USMomentCommentCell ()

@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UILabel *contentLab;

@end

@implementation USMomentCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    self.bgView = [[UIView alloc]init];
    [self.contentView addSubview:self.bgView];
    self.bgView.backgroundColor = [UIColor jk_colorWithHex:0xE6E6DA andAlpha:0.6];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(24);
        make.right.mas_equalTo(-24);
        make.top.bottom.mas_equalTo(0);
    }];
    
    self.nameLab = [[UILabel alloc]init];
    [self.bgView addSubview:self.nameLab];
    self.nameLab.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    self.nameLab.textColor = UIColor.titleColor;
    self.nameLab.textAlignment = NSTextAlignmentLeft;
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(8);
        make.height.mas_equalTo(16);
    }];
    
    self.timeLab = [[UILabel alloc]init];
    [self.bgView addSubview:self.timeLab];
    self.timeLab.font = [UIFont systemFontOfSize:12];
    self.timeLab.textColor = [UIColor jk_colorWithHex:0x212121 andAlpha:0.2];
    self.timeLab.textAlignment = NSTextAlignmentRight;
    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLab).offset(0);
        make.right.mas_equalTo(-8);
    }];
    
    
    self.contentLab = [[UILabel alloc]init];
    [self.bgView addSubview:self.contentLab];
    self.contentLab.font = [UIFont systemFontOfSize:14];
    self.contentLab.textColor = UIColor.titleColor;
    self.contentLab.textAlignment = NSTextAlignmentLeft;
    self.contentLab.numberOfLines = 0;
    [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLab.mas_bottom).offset(8);
        make.left.mas_equalTo(8);
        make.right.mas_equalTo(-8);
        make.bottom.mas_equalTo(-10);
    }];
    
    self.lineView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"lineVector"]];
    [self.contentView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8 + 24);
        make.right.mas_equalTo(-8 - 24);
        make.height.mas_equalTo(2);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.bgView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(bubbleViewLongPressAction:)]];

}


//气泡长按
- (void)bubbleViewLongPressAction:(UILongPressGestureRecognizer *)aLongPress{
    if (aLongPress.state == UIGestureRecognizerStateBegan) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(commentCellDidLongPress:bubbleView:)]) {
            [self.delegate commentCellDidLongPress:self bubbleView:self.bgView];
        }
      
    }
}

- (void)setBreakMode:(BOOL)breakMode {
    _breakMode = breakMode;
    self.contentLab.numberOfLines = breakMode ? 1 : 0;
}

- (void)setModel:(USMomentCommentModel *)model {
    _model = model;
    
    NSString *name = USUser.user.nickname;
    if (![model.userId isEqualToString: USUser.user.userId]) {
        name = USUser.user.relUser.nickname;
    }
    self.nameLab.text = name;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.gmtCreate];
    NSString *dayStr = [date jk_stringWithFormat:@"MM-dd HH:mm"];
    self.timeLab.text = dayStr;

    
    if (self.breakMode) {
        self.contentLab.text = model.content?:@"";

    }else {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 6;
        NSDictionary *attributes = @{
            NSParagraphStyleAttributeName: paragraphStyle,
        };

        NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:model.content?:@"" attributes:attributes];
        self.contentLab.attributedText = attributedText;
    }

    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
